import { AUTH_TOKEN } from "$lib/config/variable";
import { json } from "@sveltejs/kit";
import jwt from "$lib/utils/jwt";
import usersModule from "$lib/modules/users";
import permissionsModule from "$lib/modules/permissions";

export async function GET({ request, cookies }) {
  const token = cookies.get(AUTH_TOKEN);
  if (!token) { return json({ message: "未登录", status: false }, { status: 200 }); };
  const verifiedToken = await jwt.verify(token);
  if (!verifiedToken) { return json({ message: "登录已过期", status: false }, { status: 200 }); };
  const { payload: {username} } = verifiedToken;
  const user = await usersModule.getByUsername(username);
  const userinfo = await permissionsModule.getByRoleId(user.role_id);
  return json({ message: "已登录", status: true, data: {id: user.id, username, userinfo} }, { status: 200 });
}